package com.smartTerminal.utils;

import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class UploadUtil {

	/**
	 * 处理post请求
	 * @method	post
	 * @author	Xia.Zhuming
	 * @data	2018年1月11日
	 * @param url	webservice地址
	 * @param serverFieldName	文件名称(hdfs上的文件名称,如2.png)
	 * @param file
	 * @param params
	 * @return
	 * @throws Exception
	 */
    public static String post(String url, String serverFieldName, File file, Map<String, String> params) throws Exception {  
    	String respStr = null;
    	//实例化httpClient  
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        //实例化post方法  
	    HttpPost httpPost = new HttpPost(url+"?type=1");   
	    FileBody binFileBody = new FileBody(file); 
	    MultipartEntity multipartEntity = new MultipartEntity();
	    // 设置上传的其他参数  
	    setUploadParams(multipartEntity, params);
	    multipartEntity.addPart(serverFieldName, binFileBody);
	    httpPost.setEntity(multipartEntity);
	    CloseableHttpResponse response = httpclient.execute(httpPost);
	    
	    HttpEntity resEntity = response.getEntity();  
	    respStr = getRespString(resEntity);  
	    EntityUtils.consume(resEntity);  
		response.close();
		httpclient.close();
        return respStr;  
    }  
    
    /**
     * post多文件分批上传文件上传
     * @method	post
     * @author	Xia.Zhuming
     * @data	2018年1月22日
     * @param url	webservice地址
     * @param list<Map<String, File>>	文件集合
     * @param params
     * @return
     * @throws Exception
     */
    public static String post(String url, List<Map<String, File>> list, Map<String, String> params) throws Exception {  
    	String respStr = null;
    	CloseableHttpClient httpClient = HttpClients.createDefault();
    	HttpPost httpPost = new HttpPost(url + "?type=1");
    	MultipartEntity multipartEntity = new MultipartEntity();
    	setUploadParams(multipartEntity, params);
    	for(Map<String, File> map : list){
    		Set<String> set = map.keySet();
    		Iterator<String> it = set.iterator(); 
    		while (it.hasNext()) {
				String serverFiledName = it.next();
				FileBody binFileBody = new FileBody(map.get(serverFiledName)); 
				multipartEntity.addPart(serverFiledName, binFileBody);
			}
    	}
    	httpPost.setEntity(multipartEntity);
    	CloseableHttpResponse response = httpClient.execute(httpPost);
    	HttpEntity resEntity = response.getEntity();  
	    respStr = getRespString(resEntity);  
	    EntityUtils.consume(resEntity);  
		response.close();
		httpClient.close();
    	return respStr;
    }
    
    /**
     * 封装上传参数
     * @method	setUploadParams
     * @author	Xia.Zhuming
     * @data	2018年1月12日
     * @param multipartEntity
     * @param params
     * @throws Exception
     */
    private static void setUploadParams(MultipartEntity multipartEntity,  
            Map<String, String> params) throws Exception {  
        if (params != null && params.size() > 0) {  
            Set<String> keys = params.keySet();  
            for (String key : keys) {  
            	multipartEntity.addPart(key, new StringBody(params.get(key),Charset.forName("UTF-8")));
            }  
        }  
    } 
    
    /**
     * 将返回结果转化为String
     * @method	getRespString
     * @author	Xia.Zhuming
     * @data	2018年1月12日
     * @param entity
     * @return
     * @throws Exception
     */
    private static String getRespString(HttpEntity entity) throws Exception {  
        if (entity == null) {  
            return null;  
        }  
        InputStream is = entity.getContent();  
        StringBuffer strBuf = new StringBuffer();  
        byte[] buffer = new byte[4096];  
        int r = 0;  
        while ((r = is.read(buffer)) > 0) {  
            strBuf.append(new String(buffer, 0, r, "UTF-8"));  
        }  
        return strBuf.toString();  
    }  
}
